package com.king01.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.king01.dao.HomeDao;
import com.king01.entity.Staff;
import com.king01.service.HomeService;
import com.king01.util.Ossutils;
import com.king01.vo.HomeVo;
import com.king01.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：lqs
 * @date ：Created in 2023/10/11 14:26
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private HttpSession session;

    @Override
    public Result findStaffname() {
        Staff staff= (Staff) session.getAttribute("staff");
        HomeVo staffname = homeDao.findStaffname(staff.getStaffid());
        return new Result(200,"",staffname);
    }

    @Override
    public Result findBranchname() {
        Staff staff= (Staff) session.getAttribute("staff");
        HomeVo branchname = homeDao.findBranchname(staff.getBranchid());

        return new Result(200,"",branchname);

    }

    @Override
    public Result branchcount() {
        HomeVo branchcount = homeDao.branchcount();
        return new Result(200,"",branchcount);
    }

    @Override
    public Result staffcount() {
        Staff staff= (Staff) session.getAttribute("staff");
        HomeVo staffcount = homeDao.staffcount(staff.getBranchid());
        return new Result(200,"",staffcount);
    }

    @Override
    public Result goodcount() {
        Staff staff= (Staff) session.getAttribute("staff");
        HomeVo goodcount = homeDao.goodcount(staff.getBranchid());
        return new Result(200,"",goodcount);
    }

    @Override
    public Result findGoods() {
        List<HomeVo> goods = homeDao.findGoods();
        System.out.println("xxxxxxxxxxxxxx"+goods);

        List<List<Object>> list = new ArrayList<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        List<Object> list3 = new ArrayList<>();


        for (HomeVo good : goods) {
            if (good.getGoodsname()!=null) {
                list1.add(good.getGoodsname());
                list2.add(good.getCprice());
                list3.add(good.getSprice());
            }
        }
        list.add(list1);
        list.add(list2);
        list.add(list3);
        System.out.println("xxxxxxxxxxxxxx"+list);

        return new Result(200,"",list);
    }

    @Override
    public Result findDept() {
        List<Map> dept = homeDao.findDept();
        return new Result(200,"",dept);
    }

    @Override
    public Result Goods() {
        Staff staff= (Staff) session.getAttribute("staff");
        List<HomeVo> goods = homeDao.Goods(staff.getBranchid());
        List<List<Object>> list = new ArrayList<>();

        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();

        for (HomeVo good : goods) {
            list1.add(good.getGoodsname());
            list2.add(good.getCounts());
        }

        list.add(list1);
        list.add(list2);

        return new Result(200,"",list);
    }

    @Override
    public Result jbxx() {
        Staff staff= (Staff) session.getAttribute("staff");
        Staff jbxx = homeDao.jbxx(staff.getStaffid());
        return new Result(200,"",jbxx);
    }

    @Override
    public Result updatemima(Integer currentPassword,Integer newPassword) {
        Staff staff= (Staff) session.getAttribute("staff");
        List<Staff> updatemima = homeDao.updatemima(currentPassword, staff.getStaffid());
        System.out.println("pppppp"+updatemima);
        if (updatemima.isEmpty()) {
            System.out.println("[[[[[[[[");
            return new Result(500, "原始密码错误,修改失败", null);
        }
        Integer integer = homeDao.updataPassword(newPassword, staff.getStaffid());
        if (integer>0){
            return new Result(200,"密码修改成功",null);

        }else {
            return new Result(500,"密码修改失败",null);

        }
    }

    @Override
    public Result upload(MultipartFile file) {
        String path = Ossutils.ossUpload(file);
        Staff staff = (Staff) session.getAttribute("staff");
        Integer staffid = staff.getStaffid();
        homeDao.updateImg(path, staffid);
        return new Result(200,"成功",path);
    }

}

